package com.weiyuwqj.admin.controller.admin.account;

import com.weiyuwqj.admin.controller.base.BaseController;
import com.weiyuwqj.admin.controller.admin.log.annotation.LogAnnotation;
import com.weiyuwqj.api.service.*;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.exception.CrowdException;
import com.weiyuwqj.api.model.BasUser;
import com.weiyuwqj.api.model.BusAccount;
import com.weiyuwqj.api.model.SysUser;
import com.weiyuwqj.api.model.convert.AccountCashStatusConvert;
import com.weiyuwqj.api.model.convert.ModelConvert;
import com.weiyuwqj.api.model.vo.BusAccountVo;
import com.weiyuwqj.api.model.vo.BusBonusRebateVo;
import com.weiyuwqj.api.util.ExcelUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;


/**
 * The function of saving, modifying and deleting the site module
 * <p>
 * Copyright 2015-2019 Yunfucloud technology co., Ltd.
 * Created by hanling on 15/10/19.
 */
@Controller
@RequestMapping("/admin/account")
public class AccountController extends BaseController {

    @Autowired
    private BusAccountService busAccountService;

    @Autowired
    private BusBonusRebateService bonusRebateService;

    @Autowired
    private BasUserService basUserService;

    @Autowired
    private SysUserService sysUserService;



    /**
     * 查找某一个用户的account信息
     *
     * @param userId
     * @return 返回account对象
     */
    @ResponseBody
    @RequestMapping(value = "/find", method = RequestMethod.POST)
    public BusAccount find(Integer userId) {
        return busAccountService.getBusAccountByUserId(userId);
    }

    /**
     * 列出所有用户资金记录
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Object list(
            @ModelAttribute Paginator p,
            @RequestParam(defaultValue = "") String commonCond,
            @RequestParam(defaultValue = "") String userId,
            String orderName,
            String orderWay) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("commonCond", commonCond);
        map.put("userId", userId);
        if (StringUtils.isNotEmpty(orderName)) {
            map.put("orderName", orderName);
        }
        if (StringUtils.isNotEmpty(orderWay)) {
            map.put("orderWay", orderWay);
        }
        return busAccountService.getList(p, map);
    }


    /**
     * 用户资金导出
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/excel/account")
    @LogAnnotation(module = "账户管理/用户资金", description = "用户资金表导出")
    public Object excelList(HttpServletRequest request, String commondCond, String userId) {

        String[] cArray = {"Id", "用户名", "真实姓名", "总金额", "可用金额", "冻结金额", "待收金额", "待还金额"};
        String[] eArray = {"Id", "Username", "Realname", "Total", "Usable", "Frozen", "Wait", "Repay"};
        ModelConvert[] converts = {null, null, null, null, null, null, null, null};

        List<String> cList = Arrays.asList(cArray);
        List<String> eList = Arrays.asList(eArray);
        List<ModelConvert> convertList = Arrays.asList(converts);
        Map map = new HashMap();
        map.put("commonCond", commondCond);
        map.put("userId", userId);

        List<BusAccountVo> contentList = busAccountService.getList(map);

        String serverDir = request.getServletContext().getRealPath("/") + "admin/export/";
        String excelFilePath = ExcelUtils.export("用户资金", cList, eList, convertList, contentList, serverDir);
        HashMap ret = Message.OK;
        ret.put("file", "export/" + excelFilePath);
        return ret;
    }

    /**
     * 统计资金
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/statAccount")
    public Object statAccount(@RequestParam(defaultValue = "") String commonCond,
                              @RequestParam(defaultValue = "") String userId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("commonCond", commonCond);
        map.put("userId", userId);
        return busAccountService.statAccount(map);
    }



//    /**
//     * 平台转个人
//     *
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping("/transferPlatform2Person")
//    public Object transferPlatform2Person(Integer toId, String password, BigDecimal amount, Integer causeType) {
//        return bonusRebateService.transferPlatform2Person(toId, amount, password, causeType);
//    }

//    /**
//     * 平台账户互转
//     *
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping("/PlatformAccountConverse")
//    public Object PlatformAccountTransfer(Integer fromType, Integer toType, String password, BigDecimal amount) {
//        return bonusRebateService.platformAccountTransfer(fromType, toType, amount, password);
//    }



    //查询后台担保公司和平台用户余额
    @ResponseBody
    @RequestMapping(value = "/getByUserId", method = RequestMethod.POST)
    public BusAccount getByUserId() {
        Integer userId = this.getSessionUserId();
        return busAccountService.getBusAccountByUserId(userId);
    }


    /**
     * 公司账单
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/listServiceRebate")
    public Object listServiceRebate(Paginator p,
                                    String orderNo,
                                    String payUsername,
                                    String receiveUsername,
                                    String payRealname,
                                    String recevieRealname,
                                    String status,
                                    String operType,
                                    String beginTime,
                                    String endTime) {
        Map map = new HashMap();
        map.put("orderNo", orderNo);
        map.put("payUsername", payUsername);
        map.put("receiveUsername", receiveUsername);
        map.put("payRealname", payRealname);
        map.put("recevieRealname", recevieRealname);
        map.put("status", status);
        map.put("operType", operType);
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        return bonusRebateService.getList(p, map);
    }

    /**
     * 用户资金导出
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/excel/serviceRebate")
    public Object excelServiceRebate(HttpServletRequest request,
                                     String orderNo,
                                     String payUsername,
                                     String receiveUsername,
                                     String payRealname,
                                     String recevieRealname,
                                     String status,
                                     String operType,
                                     String beginTime,
                                     String endTime) {

        String[] cArray = {"支付方", "收账方", "流水号", "金额(元)", "备注", "状态", "添加时间"};
        String[] eArray = {"PayUsername", "ReceiveUsername", "OrderNo", "Amount", "Remark", "Status", "Addtime"};
        ModelConvert[] converts = {null, null, null, null, null, new AccountCashStatusConvert(), null};

        List<String> cList = Arrays.asList(cArray);
        List<String> eList = Arrays.asList(eArray);
        List<ModelConvert> convertList = Arrays.asList(converts);
        Map map = new HashMap();
        map.put("orderNo", orderNo);
        map.put("payUsername", payUsername);
        map.put("receiveUsername", receiveUsername);
        map.put("payRealname", payRealname);
        map.put("recevieRealname", recevieRealname);
        map.put("status", status);
        map.put("operType", operType);
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        List<BusBonusRebateVo> contentList = bonusRebateService.getList(map);

        String serverDir = request.getServletContext().getRealPath("/") + "admin/export/";
        String excelFilePath = ExcelUtils.export("公司账单", cList, eList, convertList, contentList, serverDir);
        HashMap ret = Message.OK;
        ret.put("file", "export/" + excelFilePath);
        return ret;
    }

    /**
     * 根据用户id查询用户
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/getUsernameById")
    public Object getUsernameById(Integer userId) {
        Map map = new HashMap();
        BasUser basUser = basUserService.selectByKey(userId);
        if (basUser != null) {
            map.put("result", "success");
            map.put("username", basUser.getUsername());
            return map;
        }
        SysUser sysUser = sysUserService.selectByKey(userId);
        if (sysUser != null) {
            map.put("result", "success");
            map.put("username", sysUser.getSysUserName());
            map.put("realname", sysUser.getSysUserName());
            return map;
        }
        if (basUser == null && sysUser == null) {
            throw new CrowdException("user.login.username.no_such_user");
        }
        return Message.FAIL;
    }


}
